package com.sub.structure.example;

import java.util.function.Consumer;

public class LinkedList<E> {


    private static class Node<E> {
        E item;
        Node<E> next;

        Node(E item, Node<E> next) {
            this.item = item;
            this.next = next;
        }
    }

    private int size = 0;

    private Node<E> head;

    private Node<E> tail;

    /**
     * 获取当前元素个数
     *
     * @return 个数
     */
    public int size() {
        return size;
    }

    /**
     * 判断链表是否为空
     *
     * @return 是否为空
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在链表末尾添加元素
     *
     * @param element 要添加的新元素
     */
    public void add(E element) {

        add(size, element);
    }

    /**
     * 在指定位置插入元素
     *
     * @param index   指定位置
     * @param element 将要插入的新元素
     */
    public void add(int index, E element) {

        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }

        if (isEmpty()) {
            Node<E> node = new Node<>(element, null);
            head = node;
            tail = node;
        } else {
            if (index == 0) {
                head = new Node<>(element, head);
            } else if (index == size) {
                Node<E> node = new Node<>(element, null);
                tail.next = node;
                tail = node;
            } else {
                Node<E> pre = getNode(index - 1);
                Node<E> next = pre.next;
                pre.next = new Node<>(element, next);
            }
        }
        size++;
    }

    private Node<E> getNode(int index) {
        Node<E> current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        return current;
    }

    /**
     * 获取指定位置的元素
     *
     * @param index 指定位置
     * @return 指定位置的元素
     */
    public E get(int index) {

        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }

        Node<E> node = getNode(index);

        return node.item;
    }

    /**
     * 删除指定位置的元素
     *
     * @param index 指定位置
     * @return 删除的元素
     */
    public E remove(int index) {

        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }

        E result;

        if (index == 0 && index == size - 1) {
            result = head.item;
            head = null;
            tail = null;
        } else if (index == 0) {
            result = head.item;
            head = head.next;
        } else if (index == size - 1) {
            result = tail.item;
            Node<E> pre = getNode(index - 1);
            pre.next = null;
            tail = pre;
        } else {
            Node<E> pre = getNode(index - 1);
            Node<E> current = pre.next;
            result = current.item;
            pre.next = current.next;
        }
        size--;
        return result;
    }

    /**
     * 修改指定位置的元素
     *
     * @param index   指定位置的索引
     * @param element 新元素
     * @return 旧元素
     */
    public E set(int index, E element) {

        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }

        Node<E> node = getNode(index);
        E result = node.item;
        node.item = element;

        return result;
    }

    /**
     * 遍历列表
     *
     * @param consumer 对元素的处理函数
     */
    public void forEach(Consumer<E> consumer) {
        Node<E> current = head;
        while (current != null) {
            consumer.accept(current.item);
            current = current.next;
        }
    }

    /**
     * 链表内容展示形式为：[1->4->2->3->5->null]
     *
     * @return 链表所有元素
     */
    @Override
    public String toString() {

        if (size == 0) {
            return "[]";
        }

        StringBuilder sb = new StringBuilder();
        sb.append("[");

        forEach(e -> sb.append(e).append("->"));

        sb.append("null]");

        return sb.toString();
    }
}